package com.hlz.rabbit.mq.pubsub;

import com.hlz.rabbit.mq.util.RabbitMqUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 发布订阅模式的消费者
 *
 * @author Hongliang Zhu
 * @create 2022-11-20 17:00
 */
public class Consumer1FroPubSub {

    public static void main(String[] args) {
        try{
            Channel channel = RabbitMqUtils.getChannel();
            channel.basicConsume("test-fanout-queue1", true, new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                    System.out.println("consumerTag:" + consumerTag);
                    System.out.println("Exchange:" + envelope.getExchange());
                    System.out.println("DeliveryTag:" + envelope.getDeliveryTag());
                    System.out.println("RoutingKey:" + envelope.getRoutingKey());

                    System.out.println("message: "+ new String(body));
                }
            });

        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
